package com.esdk.utils;

import java.io.File;
import java.io.FilenameFilter;
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.TreeMap;

public class DeleteRedundancyFile{

	private TreeMap<String,Object> reserveFiles=new TreeMap<>();
	private TreeMap<String,File> deleteFiles=new TreeMap<>();

	// 对外提供
	public static void exec(String path,String dbName){
		new DeleteRedundancyFile().doFile(path,dbName);
	}

	private void doFile(String path,String dbName){
		// 初始化文件
		File[] files=new File(path).listFiles(new FilenameFilter(){
			@Override
			public boolean accept(File dir,String name){
				return name.startsWith(dbName);
			}
		});

		// 过滤一周之内的文件(即：默认不删除一周内的文件)
		List<File> firstWeekFiles=new ArrayList<>();
		// 处理一周之外的文件
		List<File> dealFiles=new ArrayList<>();
		LocalDate firstWeek=LocalDate.now().minusWeeks(1);

		Arrays.stream(files).sorted(Comparator.comparing(File::lastModified).reversed()).forEach(e -> {
			if(firstWeek.isBefore(millToDate(e.lastModified())))
				firstWeekFiles.add(e);
			else
				dealFiles.add(e);
		});
		reserveFiles.put("week_1",firstWeekFiles);

		for(File file:dealFiles){
			// 处理周
			boolean week=dealWeek(file);
			// 处理月份
			if(!week)
				dealMonth(file);
		}
		// 删除文件
		deleteFile();
	}

	// 删除文件
	private void deleteFile(){
		System.out.println("***************** 保留文件  *****************");
		// 保留的文件
		for(Iterator<String> item=reserveFiles.keySet().iterator();item.hasNext();){
			String key=item.next();
			Object list=reserveFiles.get(key);
			if(list instanceof List){
				List<File> fs=(List<File>)list;
				fs.forEach(e -> System.out.println("日期："+millToDate(e.lastModified())+"  | 文件名："+e.getName()));
			}else{
				File file=(File)list;
				System.out.println("日期："+millToDate(file.lastModified())+"  | 文件名："+file.getName());
			}
		}

		System.out.println("****************** 删除文件 ****************");

		// 删除的文件
		for(Iterator<String> item=deleteFiles.keySet().iterator();item.hasNext();){
			String key=item.next();
			File file=deleteFiles.get(key);
			System.out.println("日期："+millToDate(file.lastModified())+"  | 文件名："+file.getName());
			file.delete();
		}
	}

	// 处理周
	private boolean dealWeek(File file){
		LocalDate nowDate=LocalDate.now();
		LocalDate fileDate=millToDate(file.lastModified());
		boolean isWeek=false;
		// 处理周
		for(int i=2;i<5;i++){
			LocalDate beforeWeek=nowDate.minusWeeks(i);
			LocalDate afterWeek=nowDate.minusWeeks(i-1);
			// beforeWeek < fileDate <= afterWeek
			if(beforeWeek.isBefore(fileDate)&&afterWeek.isAfter(fileDate)||afterWeek.equals(fileDate)){
				if(reserveFiles.containsKey("week_"+i))
					deleteFiles.put(file.getName(),file);
				else
					reserveFiles.put("week_"+i,file);

				isWeek=true;
				break;
			}
		}
		return isWeek;
	}

	// 处理月份
	private void dealMonth(File file){
		LocalDate nowDate=LocalDate.now();
		LocalDate fileDate=millToDate(file.lastModified());
		// 处理月份
		for(int i=1;i<=12;i++){
			LocalDate afterMonth=nowDate.minusMonths(i);
			LocalDate beforeMonth=nowDate.minusMonths(i+1);
			// beforeMonth < fileDate <= afterMonth
			if(beforeMonth.isBefore(fileDate)&&afterMonth.isAfter(fileDate)||afterMonth.equals(fileDate)){
				if(reserveFiles.containsKey("month_"+i))
					deleteFiles.put(file.getName(),file);
				else
					reserveFiles.put("month_"+i,file);
				break;
			}else if(i>=12&&fileDate.isBefore(beforeMonth))
				deleteFiles.put(file.getName(),file); // 删除一年之外的文件
		}
	}

	private LocalDate millToDate(Long millisecond){
		return Instant.ofEpochMilli(millisecond).atZone(ZoneId.of("Asia/Shanghai")).toLocalDate();
	}
}
